package cn.zebra.dev.admin.module.admin.controller;

import cn.zebra.dev.admin.module.admin.vo.RoleListQueryVO;
import cn.zebra.dev.admin.module.login.SecurityAdminService;
import cn.zebra.dev.admin.module.role.entity.AdminRoleEntity;
import cn.zebra.dev.admin.module.role.service.AdminRoleMenuService;
import cn.zebra.dev.admin.module.role.service.AdminRoleService;
import cn.zebra.dev.base.module.admin.entity.BaseAdmin;
import cn.zebra.dev.common.constant.ParamsConstant;
import cn.zebra.dev.common.params.RequestArrayParams;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.common.utils.ObjectUtils;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author runnable@sina.cn
 */
@RestController
@Api(tags = "角色管理")
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private AdminRoleService roleService;

    @Autowired
    private AdminRoleMenuService roleMenuService;

    @Autowired
    private SecurityAdminService securityService;

    @GetMapping("/list")
    @ApiOperation("角色列表")
    public Result<PageUtils<AdminRoleEntity>> list(
            RoleListQueryVO vo,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1") Integer page) {
        //如果不是超级管理员，则只查询自己创建的角色列表
        BaseAdmin baseAdmin = securityService.getBaseAdmin();
        Map<String, Object> params = ObjectUtils.objectConvertMap(vo);
        params.put(ParamsConstant.LIMIT, limit);
        params.put(ParamsConstant.PAGE, page);
        if (!baseAdmin.getRoleIds().contains(1L)) {
            params.put("createId", baseAdmin.getId());
        }
        PageUtils<AdminRoleEntity> pageUtils = roleService.findPage(params);
        return Result.success(pageUtils);
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取角色信息")
    public Result<AdminRoleEntity> get(@PathVariable Long id) {
        AdminRoleEntity role = roleService.selectOne(id);
        //查询角色对应的菜单
        List<Long> menuIdList = roleMenuService.queryMenuIdList(id);
        role.setMenuIdList(menuIdList);
        return Result.success(role);
    }

    @GetMapping("/select")
    @ApiOperation("角色选择列表")
    public Result<Collection<AdminRoleEntity>> select() {
        Map<String, Object> queryParams = new HashMap<>(1);
        BaseAdmin baseAdmin = securityService.getBaseAdmin();
        if (!baseAdmin.getRoleIds().contains(1L)) {
            queryParams.put("create_id", baseAdmin.getId());
        }
        Collection<AdminRoleEntity> list = roleService.listByMap(queryParams);
        return Result.success(list);
    }

    @PostMapping("/save")
    @ApiOperation("保存角色")
    public Result save(@Valid @RequestBody AdminRoleEntity entity) {
        if (roleService.saveRole(entity, securityService.getId())) {
            return Result.success("添加角色成功!");
        }
        return Result.error("保存失败!");
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改角色")
    public Result update(@PathVariable Long id, @Valid @RequestBody AdminRoleEntity entity) {
        if (roleService.updateRole(id, entity, securityService.getId())) {
            return Result.success("修改成功!");
        }
        return Result.error("修改失败!");
    }

    @PostMapping("/delete")
    @ApiOperation("删除角色")
    public Result delete(@RequestBody RequestArrayParams<Long> params) {
        Collection<Long> ids = params.getArray();
        if (ids == null || ids.size() <= 0) {
            return Result.error("请选择角色!");
        }
        UpdateWrapper<AdminRoleEntity> wrapper = new UpdateWrapper<>();
        wrapper.in("id", ids);
        wrapper.set("delete_flag = {0}", true);
        if (!roleService.update(wrapper)) {
            return Result.error("删除失败!");
        }

        return Result.success("删除成功!");
    }
}
